package com.mmlc.meimei.lichengkongjian.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.inter.InterAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.ActiveInfoListResponseBean;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.XUtils;
import com.mmlc.widget.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/18.
 */
public class FashionRecommendAdapter extends InterAdapter<ActiveInfoListResponseBean.DataBean> {
    public FashionRecommendAdapter(Context context, ArrayList<ActiveInfoListResponseBean.DataBean> datas) {
        super(context, datas);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fashion_recommend, parent, false);

            holder = new ViewHolder();
            holder.rl_fashion_recommend = ViewUtils.getView(convertView, R.id.rl_fashion_recommend);
            holder.iv_avatar = ViewUtils.getView(convertView, R.id.iv_avatar);
            holder.tv_name = ViewUtils.getView(convertView, R.id.tv_name);
            holder.tv_introduce = ViewUtils.getView(convertView, R.id.tv_introduce);

            holder.list_item = ViewUtils.getView(convertView, R.id.list_item);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ActiveInfoListResponseBean.DataBean dataBean = datas.get(position);

        if (!ViewUtils.isEmpty(dataBean.getHeader_thumb()))
            XUtils.image(holder.iv_avatar, dataBean.getHeader_thumb(),
                    XUtils.getImageOptionsBuilder()
                            .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                            .setCircular(true)
                            .build());
        if (!ViewUtils.isEmpty(dataBean.getTname())) holder.tv_name.setText(dataBean.getTname());
        if (!ViewUtils.isEmpty(dataBean.getNichen()))
            holder.tv_introduce.setText(dataBean.getNichen());
        if (null != dataBean.getActiveInfoList() && dataBean.getActiveInfoList().size() > 0) {
            holder.list_item.setAdapter(new FashionItemAdapter(context, dataBean.getActiveInfoList()));
        }

        holder.rl_fashion_recommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, RecommendInfoActivity.class);
                intent.putExtra("user_id", dataBean.getId())
                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });
        holder.list_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("id", dataBean.getActiveInfoList().get(position).getId())
                        .putExtra("url", dataBean.getActiveInfoList().get(position).getLink_url())
                        .putExtra("shareImageUrl", dataBean.getActiveInfoList().get(position).getImg())
                        .putExtra("title", dataBean.getActiveInfoList().get(position).getName())
                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        .putExtra("type", 1);
//                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(new WebAddress("http://wx.lolever.com/html/info_details.html?id=" + dataBean.getActiveInfoList().get(position).getId()).toString()));
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class FashionItemAdapter extends InterAdapter<ActiveInfoListResponseBean.DataBean.ActiveInfoListBean> {
        public FashionItemAdapter(Context context, List<ActiveInfoListResponseBean.DataBean.ActiveInfoListBean> datas) {
            super(context, datas);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_fashion_recommend_more, parent, false);
            }

            ImageView iv_image = ViewUtils.ViewHolder.get(convertView, R.id.iv_image);
            TextView tv_name = ViewUtils.ViewHolder.get(convertView, R.id.tv_name);

            ActiveInfoListResponseBean.DataBean.ActiveInfoListBean activeInfoListBean = datas.get(position);
//            if (!ViewUtils.isEmpty(activeInfoListBean.getImg()))
            XUtils.image(iv_image, activeInfoListBean.getImg(),
                    XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.CENTER_CROP).build());
            if (!ViewUtils.isEmpty(activeInfoListBean.getName()))
                tv_name.setText(activeInfoListBean.getName());
            if (position == getCount() - 1)
                ViewUtils.ViewHolder.get(convertView, R.id.line_bottom).setVisibility(View.GONE);
            else
                ViewUtils.ViewHolder.get(convertView, R.id.line_bottom).setVisibility(View.VISIBLE);

            return convertView;
        }

    }

    class ViewHolder {
        RelativeLayout rl_fashion_recommend;
        ImageView iv_avatar;
        TextView tv_name, tv_introduce;

        MyListView list_item;
    }
}
